import config from "./config.js";

//小蛇的集合
export const snakes = [];

//小蛇
export default class Snake {
  //旧位置
  oldPosition = {};
  constructor(app, options) {
    this.$app = app;
    this.$options = Object.assign(config.snake, options);
    this.$el = this.element();
    snakes.push(this);
  }

  element() {
    const div = document.createElement("div");
    div.classList.add("snake");
    div.style.backgroundColor = this.$options.bgColor;
    div.style.left = this.$options.left + "px";
    div.style.top = this.$options.top + "px";
    div.style.width = config.width + "px";
    div.style.height = config.height + "px";
    return div;
  }

  //设置蛇新的位置
  set left(value) {
    this.oldPosition.left = this.left;
    this.$el.style.left = value + "px";
  }
  set top(value) {
    this.oldPosition.top = this.top;
    this.$el.style.top = value + "px";
  }

  //当前对象位置
  get left() {
    return this.$el.offsetLeft;
  }

  get top() {
    return this.$el.offsetTop;
  }

  static get first() {
    return snakes[0];
  }

  static get last() {
    return snakes[snakes.length - 1];
  }

  static get count() {
    return snakes.length;
  }
}
